package com.jxb.first;

import java.util.ArrayList;
import java.util.List;

/**
 * 类注释
 *
 * @author jiaobo
 * @date Created in 2024/11/17 17:21
 **/
public class NumbersDisappeared {

    public static void main(String[] args) {
        int[] nums = {1,2,2,3,3,4,7,6};
        List<Integer> numbersDisappeared = findNumbersDisappeared1(nums);
        System.out.println(numbersDisappeared);
        // int res = 2 % 8;
        // System.out.println(res);
    }

    //把出现过的数字变成负数，然后再进行过滤
    public static List<Integer> findNumbersDisappeared(int[] nums) {
        List<Integer> list = new ArrayList<>();
        int n = nums.length;
        for (int num:nums) {
            int index = Math.abs(num) - 1;
            if (nums[index] > 0) {
                nums[index] = -nums[index];
            }
        }
        for (int i = 0;i<nums.length;i++) {
            if (nums[i] > 0) {
                list.add(i+1);
            }
        }
        return list;
    }

    //将每个元素进行-1取模加上数组长度，最后进行过滤，若小于等于数组长度的就是缺少的元素
    //4-->{4,3,2,15,8,2,3,1}
    //3-->{4,3,10,15,8,2,3,1}
    //2-->{4,11,10,15,8,2,3,1}
    //7-->{4,11,10,15,8,2,11,1}
    //8-->{4,11,10,15,8,2,11,9}
    //2-->{4,11,10,15,8,2,11,9}
    //3-->{4,11,10,15,8,2,11,9}
    //1-->{12,11,10,15,8,2,11,9}
    //int x = (1 - 1) %8;
    //int i = nums[x] += 8;
    public static List<Integer> findNumbersDisappeared1(int[] nums) {
        List<Integer> res = new ArrayList<>();
        int length = nums.length;
        //将每个元素进行-1取模加上数组长度
        for (Integer num : nums) {
            int x = (num - 1) % length;
            nums[x] += length;
        }

        //过滤小于等于数组长度的元素
        for (int i = 0; i < nums.length;i++) {
            if (nums[i] <= length) {
                res.add(i+1);
            }
        }
        return res;
    }

}
